package org.jeecg.modules.parameter.biz.service.impl;


import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.parameter.api.entity.power.SimPower;
import org.jeecg.modules.parameter.api.vo.power.SimPowerJsonVo;
import org.jeecg.modules.parameter.api.vo.power.SimPowerWithStationVo;
import org.jeecg.modules.parameter.biz.mapper.SimLinePowerMapper;
import org.jeecg.modules.parameter.biz.mapper.SimLinePowerWithMapper;
import org.jeecg.modules.parameter.biz.service.SimLinePowerService;
import org.jeecg.modules.parameter.common.biz.service.impl.FzServiceImpl;
import org.jeecg.modules.parameter.common.utils.oConvertUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author LSY
 */
@Slf4j
@Primary
@Service
@Transactional(rollbackFor = Exception.class)
public class SimLinePowerServiceImpl extends FzServiceImpl<SimLinePowerMapper, SimPower> implements SimLinePowerService {

    @Resource
    private SimLinePowerMapper powerMapper;
    @Resource
    private SimLinePowerWithMapper powerWithMapper;

    @Override
    public List<SimPowerJsonVo> getAllPowerWith(String lineId) {
        List<SimPowerJsonVo> simPowerVos = oConvertUtils.entityListToModelList(powerMapper.getPowerByLineId(lineId), SimPowerJsonVo.class);


        List<SimPowerWithStationVo> simPowerWithStations = oConvertUtils.entityListToModelList(
                powerWithMapper.getPowerWithByLineId(lineId), SimPowerWithStationVo.class);
        if (simPowerVos != null) {
            for (SimPowerJsonVo power : simPowerVos) {
                if (simPowerWithStations != null) {
                    List<SimPowerWithStationVo> filteredList = simPowerWithStations.stream()
                            .filter(item -> power.getId().equals(item.getMainPowerId())).collect(Collectors.toList());
                    power.setPowerWithStation(filteredList);
                }
            }
        }
        return simPowerVos;
    }

    @Override
    public SimPowerJsonVo getPowerByPowerId(String id) {
        SimPowerJsonVo simPowerVos = oConvertUtils.entityToModel(powerMapper.selectById(id), SimPowerJsonVo.class);
        List<SimPowerWithStationVo> simPowerWithStations = oConvertUtils.entityListToModelList(powerWithMapper.getPowerWithByPowerId(id), SimPowerWithStationVo.class);
        assert simPowerVos != null;
        simPowerVos.setPowerWithStation(simPowerWithStations);
        return simPowerVos;
    }


    @Override
    public boolean delPowerWith(String powerId) {
        return powerWithMapper.delPowerWith(powerId);
    }

}
